package com.feldschmid.subdroid;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.feldschmid.subdroid.db.RepositoryDbAdapter;
import com.feldschmid.subdroid.db.RevisionDbAdapter;

public class Subdroid extends ListActivity {
	
    private static final int CREATE_ID = Menu.FIRST;
    private static final int MODIFY_ID = Menu.FIRST + 1;    
    private static final int DELETE_ID = Menu.FIRST + 2;
    
    private static final int ACTIVITY_CREATE = 0;
    private static final int ACTIVITY_EDIT = 1;
    
    private RepositoryDbAdapter mRepoDbHelper;
    private RevisionDbAdapter mRevDbHelper;
    
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
        mRepoDbHelper = new RepositoryDbAdapter(this);
        mRepoDbHelper.open();
        fillData();
        registerForContextMenu(getListView());
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, CREATE_ID, 0, R.string.menu_insert);
        return true;
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
        case CREATE_ID:
            createRepository();
            return true;
        }
       
        return super.onMenuItemSelected(featureId, item);
    }
    
    private void createRepository() {
        Intent i = new Intent(this, RepositoryEdit.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }
	
    @Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, MODIFY_ID, 0, R.string.menu_modify);
        menu.add(0, DELETE_ID, 0, R.string.menu_delete);
	}
    
    @Override
	public boolean onContextItemSelected(MenuItem item) {
    	long id = ((AdapterContextMenuInfo) item.getMenuInfo()).id;
    	
		switch(item.getItemId()) {
    	case DELETE_ID:
    		
    		mRevDbHelper = new RevisionDbAdapter(this);
    		mRevDbHelper.open();
    		mRevDbHelper.deleteRevisionForRepositoryId(id);
    		
	        mRepoDbHelper.deleteRepository(id);
	        fillData();
	        return true;
    	case MODIFY_ID:
            Intent i = new Intent(this, RepositoryEdit.class);
            i.putExtra(RepositoryDbAdapter.KEY_ROWID, id);
            startActivityForResult(i, ACTIVITY_EDIT);        
		}
		return super.onContextItemSelected(item);
	}
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, Revisions.class);
        i.putExtra(RevisionDbAdapter.KEY_FK_REPID, id);
        startActivity(i);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, 
                                    Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();
    }
    
	
    private void fillData() {
        // Get all of the rows from the database and create the item list
    	Cursor repoCursor = mRepoDbHelper.fetchAllNotes();
        startManagingCursor(repoCursor);
        
        // Create an array to specify the fields we want to display in the list
        String[] from = new String[]{RepositoryDbAdapter.KEY_NAME};
        
        // and an array of the fields we want to bind those fields to
        int[] to = new int[]{R.id.repository_name};
        
        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter notes = 
        	    new SimpleCursorAdapter(this, R.layout.repository_row, repoCursor, from, to);
        setListAdapter(notes);
    }
}